package es.udc.pfc.davidparedes.web.pages.user;

import java.util.ArrayList;
import java.util.Calendar;
import java.util.List;

import org.apache.tapestry5.annotations.AfterRender;
import org.apache.tapestry5.annotations.Environmental;
import org.apache.tapestry5.annotations.Property;
import org.apache.tapestry5.annotations.SessionState;
import org.apache.tapestry5.ioc.annotations.Inject;
import org.apache.tapestry5.services.javascript.JavaScriptSupport;

import com.google.gson.Gson;

import es.udc.pfc.davidparedes.model.shift.Shift;
import es.udc.pfc.davidparedes.model.store.Store;
import es.udc.pfc.davidparedes.model.storeservice.StoreService;
import es.udc.pfc.davidparedes.model.userservice.UserService;
import es.udc.pfc.davidparedes.model.util.GlobalNames;
import es.udc.pfc.davidparedes.model.util.InstanceNotFoundException;
import es.udc.pfc.davidparedes.web.services.AuthenticationPolicy;
import es.udc.pfc.davidparedes.web.services.AuthenticationPolicyType;
import es.udc.pfc.davidparedes.web.util.CalendarEvent;
import es.udc.pfc.davidparedes.web.util.UserSession;

@AuthenticationPolicy(AuthenticationPolicyType.DIRECTOR)
public class StoreUMSchedule {
    
    @SessionState(create=false)
    private UserSession userSession;
    
    @Property
    private List<Shift> shifts;
    
    @Inject
    private UserService userService;
    
    @Inject
    private StoreService storeService;
    
    @Environmental
    private JavaScriptSupport javaScriptSupport;
    
    void onActivate() throws InstanceNotFoundException {
    	
    	Store store = userService.findUserProfile(userSession.getUserProfileId()).getStore();
    	
		if (store != null) {
			Calendar startMonth = Calendar.getInstance();
			startMonth.add(Calendar.MONTH, GlobalNames.PREVIOUS_LOADED_MONTHS);
			startMonth.set(Calendar.DAY_OF_MONTH, 1);
			startMonth.set(Calendar.HOUR_OF_DAY, 0);
			startMonth.set(Calendar.MINUTE, 0);
			startMonth.set(Calendar.SECOND, 0);
			
			Calendar endMonth = Calendar.getInstance();
			endMonth.add(Calendar.MONTH, GlobalNames.FOLLOWING_LOADED_MONTHS);
			int maxDay = endMonth.getActualMaximum(Calendar.DAY_OF_MONTH);
			endMonth.set(Calendar.DAY_OF_MONTH, maxDay);
			endMonth.set(Calendar.HOUR_OF_DAY, 23);
			endMonth.set(Calendar.MINUTE, 59);
			endMonth.set(Calendar.SECOND, 59);
			
			shifts = storeService.getStoreShiftsByDate(store.getStoreId(), startMonth, endMonth);
			
		}
    }
    
    private String shiftsToJSON(){
    	ArrayList<CalendarEvent> array = new ArrayList<CalendarEvent>();
    	CalendarEvent event;
    	for(Shift shift : shifts){
    		event = new CalendarEvent();
	        event.setId(shift.getShiftId());
	        event.setTitle(shift.getManager().getFirstName()+" "+shift.getManager().getLastName());
	        event.setAllDay(false);
	        event.setStart(shift.getStart().getTime());
	        event.setEnd(shift.getEnd().getTime());
	        String color = new String();
	        switch (shift.getPermanent()) {
		        case GlobalNames.SHIFT_PERMANENT_MORNING : color = GlobalNames.COLOR_GREEN;
		        											break;
		        case GlobalNames.SHIFT_PERMANENT_EVENING : color = GlobalNames.COLOR_ORANGE;
		        											break;
		        default  : color=GlobalNames.COLOR_BLUE;
	        }
	        event.setBackgroundColor(color);
	        event.setBorderColor(GlobalNames.COLOR_GRAY);
	        event.setUrl("/schedule-manager/user/updateshift/" + shift.getShiftId());
	        event.setTarget("#updateShift");
	        array.add(event);
    	}
    	
    	Gson gson = new Gson();
	    String json = gson.toJson(array);
	    return json;
    }
    
    @AfterRender
    public void afterRender() {
    	javaScriptSupport.addScript("loadCalendar(%s)", shiftsToJSON());
    }
}
